package com.food.restaurantpro.observers;

import java.util.Observable;
import java.util.Observer;

import android.app.Activity;
import android.app.ProgressDialog;

import com.food.restaurantpro.R;
import com.food.restaurantpro.RestaurantApp;
import com.food.restaurantpro.connection.DataHelper;
import com.food.restaurantpro.connection.DataResult;
import com.food.restaurantpro.managingnetwork.ManagingNetwork;

public class EditYourPreOrderObserver implements Observer {

	private ProgressDialog _waitingDialog;
	private EditYourPreOrderObserver _me = this;
	private boolean hadRegisterObserver = false;
	private EditYourPreOrderObserverInterface mEditYourPreOrderObserverInterface;
	
	private Activity mActivity;
	private String mOrderID;

	public EditYourPreOrderObserver(EditYourPreOrderObserverInterface editYourPreOrderObserverInterface) {
		mEditYourPreOrderObserverInterface = editYourPreOrderObserverInterface;
	}

	/**
	 * register observer and addPreOrder
	 */
	public void registerObserver(Activity activity, String order_id, String order_current_date, String order_current_time, String order_date, String order_time, String order_place) {
		mActivity = activity;
		if (ManagingNetwork.isNetworkAvailable()) {
			// show progress
			showProcessDialog();
			hadRegisterObserver = true;
			mOrderID = order_id;
			DataHelper.getInstance().addObserver(_me);
			DataHelper.getInstance().editYourPreOrder(order_id, order_current_date, order_current_time, order_date, order_time, order_place);

		} else {
			mEditYourPreOrderObserverInterface.editYourPreOrderCallback(null,
					RestaurantApp.CURRENT_CONTEXT
							.getString(R.string.NETWORK_ERROR));
		}
	}

	/**
	 * must call this method in onDestroy of Activity which called
	 * registerObserver
	 */
	public void unRegisterObserver() {
		if (hadRegisterObserver) {
			hideProcessDialog();
			DataHelper.getInstance().deleteObserver(_me);
			hadRegisterObserver = false;
		}
	}

	@Override
	public void update(Observable observable, Object data) {
		// close progress
		
		if (data == null) {
			hideProcessDialog();
			return;
		}
			
		DataResult result = (DataResult) data;
		switch (result.getMethodName()) {
		case editYourPreOrder:
			if (result.getErrorCode() != null) {
				// show error mesage
				mEditYourPreOrderObserverInterface.editYourPreOrderCallback(null, result
						.getErrorCode().getErrorMessage());
			} else {
				if (result.getData() != null && "true".equalsIgnoreCase((String) result.getData())) {
					
					mEditYourPreOrderObserverInterface.editYourPreOrderCallback(
							mOrderID, null);
				} else {
					mEditYourPreOrderObserverInterface.editYourPreOrderCallback(null,
							RestaurantApp.CURRENT_CONTEXT.getResources()
									.getString(R.string.SERVER_ERROR));
				}
			}
			unRegisterObserver();
			break;
		default:
			break;
		}
		// Chu y: Observer se post notification den tat ca cac lop da register
		// observer. Va cho nao co phuong thuc trung voi phuong thuc no tra ve
		// thi xu ly
		// cho do, nhung cho khac ko xu ly, vi vay PHAI dat code o trong
		// "case method:"
		// khong dat code o ngoai nay
	}

	private void showProcessDialog() {
		if (_waitingDialog == null || !_waitingDialog.isShowing()) {
			_waitingDialog = new ProgressDialog(mActivity);
			_waitingDialog.setMessage(mActivity.getString(R.string.processing_pre_order));
			_waitingDialog.setCancelable(false);
			_waitingDialog.show();
		}
	}

	//Ham nay phai duoc goi trong onDestroy
	//Vi co truong hop dang request thi destroy activity
	//dialog se ko duoc dong neu ko dat ham nay trong onDestroy
	private void hideProcessDialog() {
		if (_waitingDialog != null && _waitingDialog.isShowing()) {
			_waitingDialog.dismiss();
		}
	}

}
